package Demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-11-25
 * Time: 11:36
 */

/**
 *  创建线程得几种方法
 */

//创建一个类继承 Thread 类

class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("这是继承Thead重写得run方法!");
    }
}

// 实现 Runnable 接口
class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("这是实现Runnable接口重写得run方法!");
    }
}

public class ThreadDemo {


    public static void main(String[] args) {
        // 继承
        MyThread myThread = new MyThread();
        myThread.start();

        // 接口  创建一个 Thread 类得实例, 掉用Thread得构造方法 将 MyRunnable 对象作为目标参数
        Thread t = new Thread(new MyRunnable());
        t.start();

        // 匿名内部类
        Thread t1 = new Thread((new MyThread())) {

            @Override
            public void run() {
                System.out.println("这是一个匿名内部类创建得一个Thread子对象");
            }
        };

        Thread t2 = new Thread(new MyRunnable()) {
            @Override
            public void run() {
                System.out.println("这事一个内名内部类创建得一个Runnable子对象");
            }
        };
        t1.start();
        t2.start();

        // lambda 表达式
        Thread t3 = new Thread(() -> {
            System.out.println("这是一个lambda表达式");
        });
        t3.start();
    }
}
